package com.example.administrator.fastdream.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fastdream.Constant;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.WXParmas;
import com.example.administrator.fastdream.date.bean.APayBean;
import com.example.administrator.fastdream.date.bean.WXBean;
import com.example.administrator.fastdream.date.presenter.APayPresenter;
import com.example.administrator.fastdream.date.presenter.WeCountPresenter;
import com.example.administrator.fastdream.date.utils.APay_v2;
import com.example.administrator.fastdream.date.utils.EventBusCode;
import com.example.administrator.fastdream.date.utils.EventBusUtil;
import com.example.administrator.fastdream.view.base.BaseActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JoinActivity extends BaseActivity implements APayPresenter.DataCallBack, WeCountPresenter.DataCallBack {


    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_cardinfo)
    TextView tv_cardinfo;
    @BindView(R.id.cb_cardpay)
    CheckBox cbCardpay;
    @BindView(R.id.tv_addcard)
    TextView tvAddcard;
    @BindView(R.id.cb_weixin_pay)
    CheckBox cbWeixinPay;
    @BindView(R.id.cb_alipay)
    CheckBox cbAlipay;
    @BindView(R.id.tv_money2)
    TextView tvMoney2;
    @BindView(R.id.ll_gopay)
    LinearLayout llGopay;
    public static final int SHOWJOINNUMBER = 100;
    private APayPresenter APayPresenter;
    IWXAPI api;
    private WeCountPresenter weCountPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join);
        ButterKnife.bind(this);
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("立即参与");
        APayPresenter = new APayPresenter(this, this);

        weCountPresenter = new WeCountPresenter(this, this);

        initListener();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        tv_cardinfo.setText("招商银行(6618****8623)");
        tvAddcard.setVisibility(View.GONE);
    }

    private void initListener() {
        cbCardpay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    cbWeixinPay.setChecked(false);
                    cbAlipay.setChecked(false);
                }
            }
        });
        cbWeixinPay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    cbCardpay.setChecked(false);
                    cbAlipay.setChecked(false);
                }
            }
        });
        cbAlipay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    cbCardpay.setChecked(false);
                    cbWeixinPay.setChecked(false);
                }
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.cb_cardpay, R.id.tv_addcard, R.id.cb_weixin_pay, R.id.cb_alipay, R.id.ll_gopay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.cb_cardpay:
                break;
            case R.id.tv_addcard:
                startActivity(new Intent(this, AddBankCardActivity.class));
                break;
            case R.id.cb_weixin_pay:
                break;
            case R.id.cb_alipay:
                break;
            case R.id.ll_gopay:
                if (!cbCardpay.isChecked() && !cbWeixinPay.isChecked() && !cbAlipay.isChecked()) {
                    Toast.makeText(this, "请选择支付方式", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (cbCardpay.isChecked()) {
                    APayPresenter.data(getIntent().getStringExtra("issue"), getIntent().getStringExtra("unit_price"), getIntent().getStringExtra("lucky_number"), "1");
                } else if (cbWeixinPay.isChecked()) {
                    weCountPresenter.data(getIntent().getStringExtra("issue"), getIntent().getStringExtra("unit_price"), getIntent().getStringExtra("lucky_number"), "3");
                } else if (cbAlipay.isChecked()) {
                    APayPresenter.data(getIntent().getStringExtra("issue"), getIntent().getStringExtra("unit_price"), getIntent().getStringExtra("lucky_number"), "2");
                }

                break;
        }
    }

    void doPay(WXParmas wx) {
        PayReq payReq = new PayReq();
        payReq.appId = wx.appid;
        payReq.partnerId = wx.partnerid;
        payReq.prepayId = wx.prepayid;
        payReq.packageValue = "Sign=WXPay";
        payReq.nonceStr = wx.noncestr;
        payReq.timeStamp = wx.timestamp;
        payReq.sign = wx.sign;
        api.sendReq(payReq);
    }

    @Override
    public void onEventMainThread(int eventCode, final Bundle bundle) {
        super.onEventMainThread(eventCode);
        if (eventCode == EventBusCode.JoinFinish) {
            finish();
            EventBusUtil.postEvent(EventBusCode.JoinF5);
        }
    }

    @Override
    public void date(APayBean dataBean) {
        if (cbWeixinPay.isChecked()) {
            finish();
            EventBusUtil.postEvent(EventBusCode.JoinF5);
        } else if (cbCardpay.isChecked()) {
            finish();
            EventBusUtil.postEvent(EventBusCode.JoinF5);
        } else {
            APay_v2.getInstance().setContext(JoinActivity.this).doPay_v2(dataBean.getData().getResponse());
        }
    }

    @Override
    public void date(WXBean dataBean) {
        doPay(new WXParmas(dataBean.getData().getResponse().getPrepayid(), dataBean.getData().getResponse().getSign(), dataBean.getData().getResponse().getNoncestr(),
                dataBean.getData().getResponse().getPartnerid(), dataBean.getData().getResponse().getAppid(), dataBean.getData().getResponse().getTimestamp()));
    }
}
